﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Gencode.Common.Generics
{
	public class TypeContainer
	{
		private Dictionary<Type, object> container = new Dictionary<Type, object>();

		public TypeContainer()
		{
		}

		/// <summary>
		/// Returns the specified object or null if the object doesn't exist.
		/// </summary>
		/// <param name="serviceType"></param>
		/// <returns></returns>
		public object GetType(Type serviceType)
		{
			object service = null;
			container.TryGetValue(serviceType, out service);
			return service;
		}

		/// <summary>
		/// Adds a type to the container's list of objects using the object's type as the key.
		/// </summary>
		/// <param name="service"></param>
		public void AddType(object service)
		{
			if(container.ContainsKey(service.GetType()))
				container.Remove(service.GetType());

			container.Add(service.GetType(), service);
		}

		/// <summary>
		/// Adds a type to the container's list of objects using the provided Type as the key.
		/// </summary>
		/// <param name="service"></param>
		public void AddType(object service, Type key)
		{
			if (container.ContainsKey(key))
				container.Remove(key);

			container.Add(key, service);
		}
	}
}
